#include "mesinkata.h"

void Ignore_Blank(FILE *f) {
	while ((CC == BLANK || CC == NEWLINE) && CC != MARK) ADV(f);
}

void StartString(FILE *f) {
	START(f);
	end = false;
	Ignore_Blank(f);
	if (CC != MARK && !mesinkarend) {
		if(CC==PETIK) CopyString2(f);
		else CopyString(f);
	}
	else {
		end = true;
		//ADV(f);
	}
}

void AdvString(FILE *f) {
	Ignore_Blank(f);
	if (CC != MARK && !mesinkarend) {
		if(CC==PETIK) CopyString2(f);
		else CopyString(f);
	}
	else {
		end = true;
		//ADV(f);
	}
}

void CopyString(FILE *f) {
	end = false;
	int i = 0;
	while ((CC != MARK && CC != NEWLINE) && CC != BLANK && !mesinkarend) {
		cString.s[i] =  CC;
		i++;
		ADV(f);
	}
	
	cString.length = i;
	cString.s[i] = '\0';
}

void CopyString2(FILE *f) {
	end = false;
	ADV(f);
	int i = 0;
	while (CC!=PETIK && CC != MARK && !mesinkarend) {
		cString.s[i] =  ((CC==NEWLINE) ? ' ' : CC);
		i++;
		ADV(f);
	}
	
	cString.length = i;
	cString.s[i] = '\0';
	ADV(f);
}

bool IsStringEqual(string k1 , string k2) {
	return IsStringStrictEqual(UpperCase(k1),UpperCase(k2));
}

bool IsStringStrictEqual(string k1 , string k2) {
	int i = 0;
	
	if (k1.length != k2.length)
		return false;
	else {
		while (i < k1.length && k1.s[i] == k2.s[i])
			i++;
		
		if (i == k1.length)
			return true;
		else
			return false;
	}

}

bool IsStringHasWhiteSpace(string s) {
	int i = 0;
	for(i=0;i<s.length;i++) {
		if(s.s[i]==' ') return true;
	}
	return false;
}

string StrCat(string s1 , string s2) {
	string s = s1;
	int i = s.length , j = 0;
	
	while (j < s2.length) {
		s.s[i] = s2.s[j];
		i++;
		j++;
	}
	s.length = i;
	s.s[i] = '\0';
	
	return s;
}

bool IsStringLt(string s1 , string s2) {
	IsStringStrictLt(UpperCase(s1),UpperCase(s2));
}

bool IsStringStrictLt(string s1, string s2) {
	bool retval;
	int i;
	
	i = 0;
	while (i < s2.length && i < s1.length && s1.s[i] == s2.s[i])
		i++;
	if (s1.s[i] < s2.s[i])
		retval = true;
	else
	if (s1.s[i] > s2.s[i])
		retval = false;
	else
		retval = s1.length < s2.length;
	
	return retval;
}

string CharToString(char *c) {
	int i = 0;
	string s;
	
	while (c[i] != '\0') {
		s.s[i] = c[i];
		i++;
	}

	s.length = i;
	s.s[i] = '\0';
	
	return s;
}

char* StringToChar(string s) {
	char *t = (char *)malloc(sizeof(char) * s.length);

	sprintf(t , "%s" , s.s);
	
	return t;
}

bool IsStringInt(string s) {
	if(s.length == 0) return false;
	int i = 0;
	int mincount = 0;
	if(s.s[0]!='-' && (s.s[0] < 48 || s.s[0] > 57)) return false;
	for(i = 1; i < s.length; i++) {
		if(s.s[i]=='-') mincount++;
		else if(s.s[i] < 48 || s.s[i] > 57) return false;
		if(mincount > 1) return false;
	}
	return true;
}

bool IsStringFloat(string s) {
	if(s.length == 0) return false;

	int i = 0, dotcount = 0;
	if(s.s[0]!='-' && (s.s[0] < 48 || s.s[0] > 57)) return false;
	for(i = 1; i < s.length; i++) {
		if(s.s[i]=='.') dotcount++;
		else if(s.s[i] < 48 || s.s[i] > 57) return false;
		if(dotcount > 1) return false;
	}
	return true;
}

bool IsStringDate(string s) {
	if(s.length == 0) return false;
	int i = 0, slashcount = 0;
	for(i = 0; i < s.length; i++) {
		if(s.s[i]=='-') slashcount++;
		else if(s.s[i] < 48 || s.s[i] > 57) return false;
		if(slashcount > 2) return false;
	}
	if(slashcount!=2) return false;
	int dd,mm,yy;
	sscanf(s.s,"%d-%d-%d", &dd,&mm,&yy);
	return IsTanggalValid(dd,mm,yy);
}

bool IsStringThnAkademik(string s) {
	if(s.length == 0) return false;
	int i = 0, dashcount = 0;
	for(i = 0; i < s.length; i++) {
		if(s.s[i]=='-') dashcount++;
		else if(s.s[i] < 48 || s.s[i] > 57) return false;
		if(dashcount > 1) return false;
	}
	if(dashcount!=1) return false;
	int aw,ak;
	sscanf(s.s,"%d-%d",&aw,&ak);
	return IsThnAkademikValid(aw,ak);
}

bool IsStringABCDE(string s) {
	if(s.length == 0) return false;
	return 	IsStringEqual(UpperCase(s),CharToString("A")) ||
			IsStringEqual(UpperCase(s),CharToString("C")) ||
			IsStringEqual(UpperCase(s),CharToString("D")) ||
			IsStringEqual(UpperCase(s),CharToString("E")) ||
			IsStringEqual(UpperCase(s),CharToString("B"));
}

int StringToInt(string s) {
	int retval;
	sscanf(s.s, "%d", &retval);
	return retval;
}

string IntToString(long x) {
	string s;
	int  i = 0;
	sprintf(s.s , "%ld" , x);
	
	while (s.s[i] != '\0') i++;
	s.length = i;
	
	return s;
}

float StringToFloat(string s) {
	float x;
	sscanf(s.s, "%f", &x);
	return x;
}

string FloatToString(float x) {
	char buff[50];
	sprintf(buff, "%f\0", x); 
	return CharToString(buff);
}

bool StringToBool(string s) {
	return IsStringEqual(s , CharToString("TRUE"));
}

string BoolToString(bool x) {
	if (x)
		return CharToString("TRUE");
	else
		return CharToString("FALSE");
}

date StringToDate(string s) {
	string temp;
	int DD, MM, YY;
	
	sscanf(s.s, "%d-%d-%d", &DD, &MM, &YY);
	
	return MakeTanggal(DD,MM,YY);
}

string DateToString(date d) {
	char buff[50];
	sprintf(buff, "%d-%d-%d", Day(d), Month(d), Year(d));
	return CharToString(buff);
}

ThnAkademik StringToThnAkademik(string s) {
	int tAwal, tAkhir;
	sscanf(s.s, "%d-%d", &tAwal, &tAkhir);
	return CreateThnAkademik(tAwal,tAkhir);
}

string ThnAkademikToString(ThnAkademik T) {
	return CharToString(ThnAkademikToChar(T));
}

string SubString(string s, int start, int length){
	string retval;
	int i;
	
	for (i = 0;i < length;i++){
		retval.s[i] = s.s[i+start];
	}
	retval.s[i] = '\0';
	retval.length = i;
	
	return retval;
}

void ToUpperCase(string *s)
{
	int i;
	for(i = 0; s->s[i]!='\0'; i++) {
		if(s->s[i] >= 97 && s->s[i] <=122) s->s[i] -= 32;
	}
}

string UpperCase(string s) {
	string S = s;
	int i;
	for(i = 0; S.s[i]!='\0'; i++) {
		if(S.s[i] >= 97 && S.s[i] <=122) S.s[i] -= 32;
	}
	return S;
}

string LowerCase(string s) {
	string S = s;
	int i;
	for(i = 0; S.s[i]!='\0'; i++) {
		if(S.s[i] >= 65 && S.s[i] <=90) S.s[i] += 32;
	}
	return S;
}

string TrimWhiteSpace(string s) {
	string S;
	int i , j;
	j = 0;
	
	for(i=0 ; i<=s.length ; i++) {
		if (s.s[i] != ' ') {
			S.s[j] = s.s[i];
			j++;
		}
	}
	S = CharToString(S.s);
	return S;
}

string DeleteSubString(string s , int pos , int length) {
	return StrCat(SubString(s , 0 , pos) , SubString(s , pos + length , s.length - (pos + length)));
}


//--------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------
// Fungsi Tambahan
// Di-import dari _ADT_Hilmans_General

int  LongStr(char *InputS){
   int L=0;
   while ((InputS[L]!='\0')&&(L<255)){
       L++;
   }
   return L;
}

int  ExistChar(char *InputS, char C){
    int L,Found=0;
    for (L=0;L< LongStr(InputS);L++){
        if (InputS[L]==C){
            Found=L+1;
            L= LongStr(InputS);           
        }
    }
    return Found;
}

int  AddChr(char InputS[], char InputC){
   int L= LongStr(InputS);
   InputS[L]=InputC;
   InputS[L+1]='\0';
   return (L+1);
}

int  AddChrF(char InputS[], char InputC){
   int L,Long= LongStr(InputS);
   for (L=Long;L>=1;L--)
      InputS[L]=InputS[L-1];
   InputS[0]=InputC;
   InputS[Long+1]='\0';
   return (Long+1);
}

int  DelChr(char InputS[]){
   int Long= LongStr(InputS);
   if (Long<=0)
       return 0;
   else {
       InputS[Long-1]=InputS[Long];
       return 1;
   }
}

int  DelChrF(char InputS[]){
   int L,Long= LongStr(InputS);
   if (Long<=0)
       return 0;
   else {
       for (L=1;L<=Long;L++){
           InputS[L-1]=InputS[L];
       } 
   }     
}

int  AddStr(char S1[], char S2[]){
   int L=0;
   int buff= LongStr(S1);
   while (S2[L]!='\0'){
       S1[buff+L]=S2[L];  
       L++;
   }   
   S1[buff+L]=S2[L];
   return L;
}

int  CopyStr(char Output[], char Input[]){
   int L=0;
   while (Input[L]!='\0'){
      Output[L]=Input[L];
      L++;   
   } 
   Output[L]='\0';
   return L;
}

int  IsNum(char InputC){
   return ((InputC>='0')&&(InputC<='9'))||(InputC=='.');
}


int  AddStrF(char S1[], char S2[]){
   int L=0,Result;
   int buff= LongStr(S2);
   char S[50];
    CopyStr(S,S1);
    CopyStr(S1,S2);
   Result= AddStr(S1,S);
   
   return Result;
}

int  GetStr(char *OutputS, char *InputS, int Start, int Stop){
   char S[50];
    CopyStr(S,"");
   int L;
   if (Start<=Stop){
     for (L=Start;L<=Stop;L++){
          AddChr(S,InputS[L]);
     }  
      CopyStr(OutputS,S);
   }
   else return 0;
}

int  SameStr(char *S1, char *S2){
    int L,B=1;
    if ( LongStr(S1)!= LongStr(S2))
       return 0;
    else{
        for (L=0;L< LongStr(S1);L++)
            if (S1[L]!=S2[L])
               B=0;
        return B; 
    }
}

int  ExistSub(char *OutputS,char *InputS){
   int L;
   int Long= LongStr(OutputS)- LongStr(InputS);
   int B=0;
   char S[50];
   
   if (Long>=0){
       for (L=0;L<=Long;L++){
            GetStr(S,OutputS,L,L+ LongStr(InputS)-1);
           if ( SameStr(S,InputS)){
               B=L+1;
               L=Long+1;                                                       
           }
       }                                
   }    
   return B; 
}

int  ExistSubLast(char *OutputS,char *InputS){
   int L;
   int Long= LongStr(OutputS)- LongStr(InputS);
   int B=0;
   char S[50];
   
   if (Long>=0){
       for (L=Long;L>=0;L--){
            GetStr(S,OutputS,L,L+ LongStr(InputS)-1);
           if ( SameStr(S,InputS)){
               B=L+1;
               L=-1;                                                       
           }
       }                                
   }    
   return B; 
}

int  IsChar(char InputC){
    return ((InputC>='A')&&(InputC<='Z'));
}   
    
int  IsCell_2(char *InputS){
    return ( IsChar(InputS[0]));
}

int  IsString (char *InputS){
    return (InputS[0]=='\'');
}

int  DelStrNum(char *OutputS, int Num){
   if (Num<= LongStr(OutputS)){
       OutputS[ LongStr(OutputS)-Num]='\0';                        
   }  
}

int  DelStrNumF(char *OutputS, int Num){
   int L,Long= LongStr(OutputS);
   if ((Num>Long)&&(Num<=0))
       return 0;
   else {
       for (L=1;L<=Num;L++){
            DelChrF(OutputS);
       } 
   } 
}

int  GetStrLastCodeStr(char *OutputS, char *InputS, char *Pattern){
   int Long= LongStr(InputS); 
   int Pos,L;
   if (!(Pos= ExistSubLast(InputS,Pattern))){
        CopyStr(OutputS,InputS);                             
   }
   else {
        GetStr(OutputS,InputS,Pos-1,Long-1); 
   }
}

char UpperChar(char C){
   if ((C>=97)&&(C<=122))  
      return (C-32);
   else 
      return C;
}


int UpperStr(char *OutputS, char *InputS){
    int L;
    for (L=0;L<=LongStr(InputS);L++)
       OutputS[L]=UpperChar(InputS[L]);
}

int GetStrToUpper(char *OutputS ,char *InputS, int Start, int Stop){
   char S[50];
    CopyStr(S,"");
   int L;
   if (Start<=Stop){
     for (L=Start;L<=Stop;L++){
          AddChr(S,UpperChar(InputS[L]));
     }  
      CopyStr(OutputS,S);
   }
   else return 0;
}

int  ExistSubNoCase(char *OutputS,char *InputS){
   int L;
   int Long= LongStr(OutputS)- LongStr(InputS);
   int B=0;
   char S[50];
   char Up[50];
   UpperStr(Up,InputS);
   
   if (Long>=0){
       for (L=0;L<=Long;L++){
            GetStrToUpper(S,OutputS,L,L+ LongStr(InputS)-1);
           if ( SameStr(S,Up)){
               B=L+1;
               L=Long+1;                                                       
           }
       }                                
   }    
   return B; 
}

void AddSpace(char *STR, char *Paramstr){
    char DATA[10][25];
    int Total;
    CopyStr(DATA[0]," sites ");
    CopyStr(DATA[1]," servers ");
    CopyStr(DATA[2]," clients ");
    CopyStr(DATA[3]," retrieve ");
    CopyStr(DATA[4]," update ");
    CopyStr(DATA[5]," insert ");
    CopyStr(DATA[6]," delete ");
    CopyStr(DATA[7]," table ");
    CopyStr(DATA[8]," rec ");
    int SPACE[10]={10,8,8,10,9,9,9,14,9}; 
    int L, Length;  
    for (L=0;L<=8;L++){
        if (ExistSubNoCase(DATA[L],Paramstr)){
            Total=SPACE[L];     
            L=10;                             
        }
        
    }
    Length=LongStr(STR);
    
    for (L=1;L<=Total-Length;L++){
        AddChr(STR,' ');
    }
}
